<template>
  <div class="xtx-goods-page" v-if="goods">
    <div class="container">
      <!-- 面包屑 -->
      <bread>
        <breadItem to="/">首页</breadItem>
        <breadItem :to="'/category/' + goods.categories[0].id">
          {{ goods.categories[0].name }}
        </breadItem>
        <breadItem :to="'/category/sub/' + goods.categories[1].id">
          {{ goods.categories[1].name }}
        </breadItem>
        <breadItem>{{ goods.name }}</breadItem>
      </bread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-image :images="goods.mainPictures"></goods-image>
          <goods-sales></goods-sales>
        </div>
        <div class="spec">
          <goods-name :goods="goods"></goods-name>
          <goods-sku :goods="goods" @change="changeSku"></goods-sku>
          <number v-model="count" :max="goods.inventory" label="数量"></number>
          <click-btn type="primary" style="margin-top: 20px" @click="insertCart"
            >加入购物车</click-btn
          >
        </div>
      </div>
      <!-- 商品推荐 -->
      <goods-relevant :goodsId="goods.id"></goods-relevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <goods-tabs :goods="goods"></goods-tabs>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <goods-warn></goods-warn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot :type="1" :goodId="goods.id"></goods-hot>
          <goods-hot :type="2" :goodId="goods.id"></goods-hot>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsRelevant from "./components/GoodsRelevant.vue";
import { findGoods } from "api/product.js";
import { ref, watch, nextTick, provide } from "vue";
import { useRoute } from "vue-router";
import GoodsImage from "./components/GoodsImage.vue";
import GoodsSales from "./components/GoodsSales.vue";
import GoodsName from "./components/GoodsName.vue";
import GoodsSku from "./components/GoodsSku.vue";
import GoodsTabs from "./components/GoodsTabs.vue";
import GoodsHot from "./components/GoodsHot.vue";
import GoodsWarn from "./components/GoodsWarn.vue";
import { useStore } from "vuex";
import Toast from "components/common/toast";
export default {
  name: "GoodsPage",
  components: {
    GoodsRelevant,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn,
  },
  setup() {
    // 获取商品详情，并渲染
    const goods = ref(null);
    const route = useRoute();
    // 为了以后点击商品详情页内的推荐商品时，组件能刷新，需要watch
    watch(
      () => route.params.id,
      (newValue) => {
        if (newValue && `/product/${newValue}` === route.path) {
          findGoods(newValue).then((res) => {
            // 销毁组件
            goods.value = null;
            // 下一帧再赋值，不然没意义，组件不会销毁
            nextTick(() => {
              goods.value = res.result;
            });
          });
        }
      },
      {
        immediate: true,
      }
    );

    const currentSku = ref(null);
    // 确认当前真实价格，因为不同规格的商品会有不同的价格
    const changeSku = (sku) => {
      if (sku.skuId) {
        goods.value.price = sku.price;
        goods.value.oldPrice = sku.oldPrice;
        goods.value.inventory = sku.inventory;
        //有值，记录，准备传给购物车
        currentSku.value = sku;
      }
    };
    // 选择数量
    const count = ref(1);

    // 提供goods给子代
    provide("goods", goods);
    const store = useStore();
    const insertCart = () => {
      //需要哪些数据：id skuId、name、attrsText picture price nowPrice selected stock count isEffective
      //判断是否选了规格
      if (currentSku.value) {
        store
          .dispatch("cart/insertCart", {
            id: goods.value.id,
            skuId: currentSku.value.skuId,
            name: goods.value.name,
            picture: goods.value.mainPictures[0],
            price: currentSku.value.price,
            nowPrice: currentSku.value.price,
            count: count.value,
            attrsText: currentSku.value.specsText,
            selected: true,
            isEffective: true,
            stock: currentSku.value.inventory,
          })
          .then(() => {
            Toast({ text: "添加购物车成功", type: "success" });
          });
      } else {
        Toast({ text: "请选择完整规格" });
      }
    };
    return { goods, changeSku, count, insertCart };
  },
};
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
</style>
